from django.shortcuts import render
from rest_framework.views import APIView
from .models import *
from .ser import *
from rest_framework.response import Response
from django.contrib.auth.hashers import make_password, check_password
from django.core.paginator import Paginator


# Create your views here.
# 班级
class ClassroomAPI(APIView):
    # 班级添加
    def post(self, request):
        ser = ClassroomSer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({"code": 200})
        else:
            print(ser.errors)
            return Response({"code": 10010})
        # 班级查看

    def get(self, request):
        room = Classroom.objects.all()
        ser = ClassroomSer(room, many=True)
        return Response({"list": ser.data})


# 学生
class StudentAPI(APIView):
    # 学生添加
    def post(self, request):
        ser = StudentSer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({"code": 200})
        else:
            print(ser.errors)
            return Response({"code": 10010})
        # 学生查看

    def get(self, request):
        stu = Student.objects.all()
        ser = StudentSer(stu, many=True)
        return Response({"list": ser.data})


# 注册
class UsersAPI(APIView):
    def post(self, request):
        request.data['password'] = make_password(request.data.get("password"))
        ser = UsersSer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({"code": 200})
        else:
            print(ser.errors)
            return Response({"code": 10010})


# 登录
class DlAPI(APIView):
    def post(self, request):
        usersname = request.data.get("usersname")
        password = request.data.get("password")
        user = Users.objects.filter(usersname=usersname).first()
        if user:
            if check_password(password, user.password):
                return Response({"code": 200, "userid": user.id})
            else:
                return Response({"code": 10010, "mes": "密码错误"})
        else:
            return Response({"code": 20010, "mes": "用户不存在"})

    # 反向查寻
    def get(self, request):
        room = Classroom.objects.all()
        ser = ShowClassroomSer(room, many=True)
        return Response({"code": 200, "list": ser.data})





# 商品分类
class ShopAPI(APIView):
    def post(self, request):
        ser = ShopSer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({"code": 200})

    def get(self, request):
        shop = Shoop.objects.all()
        ser = ShopSer(shop, many=True)
        return Response({"list": ser.data})

# 商品
class ShoopsAPI(APIView):
    def post(self, request):
        ser = ShopsSer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({"code": 200})

    def get(self, request):
        shops = Shoops.objects.all()
        f = Paginator(shops, 2)
        pages = request.GET.get('pages')
        p = f.page(pages)
        ser = ShopsSer(p, many=True)
        return Response({"list": ser.data, "num": f.num_pages})

class AusersAPI(APIView):
    def post(self, request):
        request.data['password'] = make_password(request.data.get('password'))
        ser = aUsersSer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({"code": 200})
        else:
            print(ser.errors)
            return Response({"code": 20010})

class usersAAPI(APIView):
    def post(self, request):
        usersname = request.data.get("usersname")
        password = request.data.get("password")
        use = aUsers.objects.filter(usersname=usersname).first()
        if use:
            if check_password(password, use.password):
                return Response({"code": 200, "userid": use.id})
            else:
                return Response({"code": 20010, "mes": "密码错误"})
        else:
            return Response({'code': 10010, "mes": "用户不存在"})

class PjAPI(APIView):
    def post(self, request):
        good = request.data.get('good')
        ser = PjSer(data=request.data)
        if ser.is_valid():
            ser.save()
            ashops = Shoops.objects.filter(id=good).first()
            ashops.count += 1
            ashops.save()
            return Response({"code": 200})
        else:
            print(ser.errors)
            return Response({"code": 10010})

    def get(self, request):
        id = request.GET.get('id')
        agoods = Shoops.objects.filter(id=id).first()
        ser = ShowShopSer(agoods)
        return Response({"code": 200, "list": ser.data})

# 图书分页
class AbookAPI(APIView):
    def get(self, request):
        pages = request.GET.get('pages')
        abook = Abook.objects.all()
        p = Paginator(abook, 2)
        book = p.page(pages)
        ser = AbookSer(book, many=True)
        return Response({"code": 200, "list": ser.data, "numm": p.num_pages})

class Getbook(APIView):
    def get(self, request):
        name = request.GET.get('name')
        abook = Abook.objects.filter(name=name).first()
        ser = AbookSer(abook)
        return Response({"code": 200, "list": ser.data})

class AdlAPI(APIView):
    def post(self, request):
        username = request.data.get('username')
        password = request.data.get('password')
        user = Author.objects.filter(username=username).first()
        if user:
            if user.password == password:
                return Response({"code": 200, "userid": user.id, "grade": user.grade})
        return Response({"code": 20010, "mes": "狗der错了"})

class SuserAPI(APIView):
    def get(self, request):
        cpage = request.GET.get('cpage')
        cc = Cshoop.objects.all()
        p = Paginator(cc, 2)
        pages = p.page(cpage)
        ser = CshoopSer(pages, many=True)
        return Response({"code": 200, "list": ser.data, "numm": p.num_pages})

# 登录
class CuserAPI(APIView):
    def post(self, request):
        username = request.data.get('username')
        password = request.data.get('password')
        user = Cuser.objects.filter(username=username).first()
        if user:
            if user.password == password:
                return Response({"code": 200, "mes": "登录成功", "userid": user.id, "gradeid": user.grade})
        return Response({"code": 10010, "mes": "错误"})


class Saak(APIView):
    def post(self, request):
        user = request.data.get('cuser')
        xx = Cuser.objects.filter(id=user).first()
        ser = SaakSer(data=request.data)
        if ser.is_valid():
            ser.save()
            if xx.grade == 1:
                xx.integral += 10
                xx.save()
                return Response({"code": 200})
            elif xx.grade == 2:
                xx.integral += 5
                xx.save()
                return Response({"code": 200})
            return Response({"code": 200})
        else:
            # print(ser.data)
            return Response({"code": 10010})

#商品
class DaAPI(APIView):
    def get(self,request):
        cpage = request.GET.get('cpage')
        mei = Mei.objects.all()
        p = Paginator(mei,2)
        alls = p.page(cpage)
        ser = ShowMeiSer(alls,many=True)
        return Response({"code":200,"list":ser.data,"numm":p.num_pages})
#登录

class SunAPI(APIView):
    def post(self,request):
        username = request.data.get('username')
        password = request.data.get('password')
        user = Qi.objects.filter(username=username).first()
        if user:
            if user.password == password:
                return Response({"code":200,"userid":user.id,"typeid":user.type})
        return Response({"code":10010,"mes":"傻逼"})

class SunssAPI(APIView):
    def post(self,request):
        ser = dashabiSer(data=request.data)
        qi = request.data.get('qi')
        axa = Qi.objects.filter(id=qi).first()
        if ser.is_valid():
            ser.save()
            axa.jifen+=10
            axa.save()
            return Response({"coe":200})
        else:
            print(ser.errors)
            return Response({"code":10010})


#收藏
class Dee(APIView):
    def post(self,request):
        id = request.GET.get('id')
        userid = request.GET.get('userid')
        print(id,userid)
        userr = Qi.objects.filter(id=userid).first()
        shoops = Mei.objects.filter(id=id).first()
        userr.sc.add(shoops)
        return Response({"code":200})

class EuserAPI(APIView):
    def post(self,request):
        ser = EuserSer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({"code:200"})
        else:
            print(ser.errors)
            return Response({"code":10010})

class EbookAPI(APIView):
    def post(self,request):
        ser = EbookSer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({"code":200})
        else:
            print(ser.errors)
            return Response({"code": 10010})
    def get(self,request):
        cpage = request.GET.get('cpage')
        ebook = Ebook.objects.all()
        p = Paginator(ebook,2)
        eall = p.page(cpage)
        ser = ShowEbookSer(eall,many=True)
        return Response({"code":200,"list":ser.data,"numm":p.num_pages})


class EdlDPI(APIView):
    def post(self,request):
        username = request.data.get('username')
        password = request.data.get('password')
        user = Euser.objects.filter(username=username).first()
        if user:
            if user.password == password:
                return Response({"code":200,"dejiid":user.deji,"userid":user.id})
        return Response({"code":10010})


class ASD(APIView):
    def get(self,request):
        book = request.GET.get('book')
        ss = Ebook.objects.filter(book=book).first()
        ser = EbookSer(ss)
        return Response({"code":200,"list":ser.data})


class MshopAPI(APIView):
    def get(self,request):
        cpage = request.GET.get('cpage')
        mshop = Mshop.objects.all()
        p = Paginator(mshop,2)
        shopall = p.page(cpage)
        ser = MshopSer(shopall,many=True)
        return Response({"code":200,"list":ser.data,"numms":p.num_pages})
    def post(self,request):
        username = request.data.get('username')
        password = request.data.get('password')
        user = Muser.objects.filter(username=username).first()
        if user:
            if user.password == password:
                return Response({"code":200,'userid':user.id})
            else:
                return Response({"code": 10010,"mes":"密码错误"})
        else:
            return Response({"code": 10011, "mes": "用户不存在"})
    def put(self,request):
        kk = request.GET.get('kk')
        id = request.GET.get('id')
        print(kk,id)
        shop = Mshop.objects.filter(id = id).first()
        if kk == 1:
            shop.number += 1
            shop.save()
        elif kk == 2:
            shop.number -= 1
            shop.save()
        # ser = MshopSer(shop,many=True)
        return Response({"code":200})






























































